/**
 * 
 */
package com.haizhi.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.haizhi.bean.TbAdmin;
import com.haizhi.bean.TbFeed;
import com.haizhi.bean.TbUser;
import com.haizhi.bean.TbUserActivity;
import com.haizhi.bean.TbUserFollow;
import com.haizhi.dao.UserDao;
import com.haizhi.service.BaseService;
import com.haizhi.service.FeedService;
import com.haizhi.service.UserActivityService;
import com.haizhi.service.UserService;
import com.querydsl.core.types.Predicate;

/**
 * @author jiangjintai
 *
 */
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<TbUser, Integer> implements UserService {
	@Autowired
	UserActivityService userActivityService;
	@Autowired
	UserDao userDao;
	@Autowired
	FeedService feedService;
	/* （非 Javadoc）
	 * @see com.haizhi.service.BaseService#findById(java.lang.Object)
	 */
	@Override
	public TbUser findById(Integer id) {
		
		return this.userDao.findOne(id);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.BaseService#save(java.lang.Object)
	 */
	@Override
	public TbUser save(TbUser entity) {
		
		return this.userDao.save(entity);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.BaseService#delete(java.lang.Object)
	 */
	@Override
	public void delete(TbUser entity) {
		this.userDao.delete(entity);
		
	}
	
	public List<TbUser> findAll(){
		return this.userDao.findAll();
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.UserService#findByUserAccount(java.lang.String)
	 */
	@Override
	public TbUser findByUserAccount(String userAccount) {
		// 
		return this.userDao.findByUserAccount(userAccount);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.UserService#findByUserPhone(java.lang.String)
	 */
	@Override
	public TbUser findByUserPhone(String userPhone) {
		// 
		return this.userDao.findByUserPhone(userPhone);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.UserService#findByUserName(java.lang.String)
	 */
	@Override
	public TbUser findByUserName(String userName) {
		// 
		return this.userDao.findByUserName(userName);
		
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.UserService#findByUserAccountAndUserPassword(java.lang.String, java.lang.String)
	 */
	@Override
	public TbUser findByUserAccountAndUserPassword(String userAccount,
			String userPassword) {
		// 
		return this.userDao.findByUserAccountAndUserPassword(userAccount,userPassword);
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.UserService#saveActivityAndAlloc(com.haizhi.bean.TbUser, java.lang.String)
	 */
	@Override
	public void saveActivityAndAlloc(TbUser tbUser, String content) {
		// 
		TbUserActivity tbUserActivity = new TbUserActivity();
		tbUserActivity.setActivityContent(content);
		tbUserActivity.setActivityTime(new Date());
		tbUserActivity.setActivityType(1);
		tbUserActivity.setTbUser(tbUser);
		userActivityService.save(tbUserActivity);
		//通知其他人
		Set<TbUserFollow> followTbUserFollowSet = this.findById(tbUser.getUserId()).getTbUserFollowsForFollowUserId(); 
		for(TbUserFollow tuf:followTbUserFollowSet){
			TbUser tbUserFans =tuf.getTbUserByUserId();//获取粉丝
			TbFeed tbFeed = new TbFeed();
			tbFeed.setFeedAddTime(new Date());
			tbFeed.setFeedIsSee(0);
			tbFeed.setFeedMessage(tbUserActivity.getActivityContent());
			tbFeed.setTbUserByTargetId(tbUser);
			tbFeed.setTbUserByUserId(tbUserFans);
			feedService.save(tbFeed);
		}
		
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.impl.BaseServiceImpl#getDao()
	 */
	@Override
	protected JpaRepository<TbUser, Integer> getDao() {
		// 
		return this.userDao;
	}

	/* （非 Javadoc）
	 * @see com.haizhi.service.UserService#findByUserAccountAndUserPasswordUseQuerydsl(com.querydsl.core.types.Predicate)
	 */
	@Override
	public TbUser findByUserAccountAndUserPasswordUseQuerydsl(Predicate p) {
		// 
		return this.userDao.findOne(p);
	}

}
